package com.BlogPark.controller;

import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.pojo.Notice;
import com.BlogPark.service.NoticeService.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 业峰
 * @Date: 2022/8/19 - 08 - 19 - 17:00
 * @version: 1.0
 * 公告发布:1.添加公告、2.查询最新公告、3.删除公告、4.查询所有公告按日期排序、5.批量删除
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 1.添加公告
     *
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    public Result addNotice(@RequestBody Notice notice) {
        Integer code;
        String msg;
        //设置文章的添加时间
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Date date = new Date(currentTime);
        notice.setDate(date);
        try {
            int row = noticeService.addNotice(notice);

            if (row != 0) {
                code = Code.SAVE_OK;
                msg = "公告添加成功";
            } else {
                code = Code.SAVE_ERR;
                msg = "公告添加失败";
            }
        } catch (Exception e) {
            code = Code.SAVE_ERR;
            msg = "公告添加失败";
            return new Result(code, null, msg);
        }

        return new Result(code, null, msg);
    }

    /**
     * 查询所有公告按日期排序
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        Integer code;
        String msg;
        List<Notice> notices = noticeService.selectAll();
        if (notices != null && !notices.isEmpty()) {
            code = Code.GET_OK;
            msg = "所有公告获取成功";
        } else {
            code = Code.GET_ERR;
            msg = "所有公告获取失败";
        }
        return new Result(code, notices, msg);
    }

    /**
     * 获取最新的公告
     *
     * @return
     */
    @GetMapping("/showLatest")
    public Result showLatest() {
        Integer code;
        String msg;
        List<Notice> notices = noticeService.selectAll();
        Notice notice = notices.get(0);
        if (notice != null) {
            code = Code.GET_OK;
            msg = "获取成功";
        } else {
            code = Code.GET_ERR;
            msg = "获取失败";
        }
        return new Result(code, notice, msg);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        Integer code;
        String msg;
        int row = noticeService.deleteById(id);
        if (row == 1) {
            code = Code.DELETE_OK;
            msg = "删除成功";
        } else {
            code = Code.DELETE_ERR;
            msg = "删除失败";
        }
        return new Result(code, null, msg);
    }

    /**
     * 批量删除公告
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByBatch")
    public Result deleteByBatch(@RequestParam Integer[] ids) {
        //System.out.println(Arrays.toString(ids));
        Integer code;
        String msg;
        int rows = noticeService.deleteByBatch(ids);
        if (rows > 0) {
            code = Code.DELETE_OK;
            msg = "批量删除成功";
        } else {
            code = Code.DELETE_ERR;
            msg = "批量删除失败";
        }
        return new Result(code,null,msg);
    }
}
